<?php

$installerExt = "phi";
$rootFolder = realpath(dirname("".__FILE__));
$searchFolder = realpath(dirname(dirname("".__FILE__)));

/*
function addFolderToIni( $folder, Ini &$ini, $root, ZipArchive &$zip ) {
	global $filesCount;
	//echo "<br/><span style=\"color:gray\">--- Start: ".$folder."</span><br/>";
	if ( !is_dir( $root.$folder ) ) return;
	
	if ( $hRootFolder = opendir( $root.$folder ) )
	{
		while( ( $file = readdir( $hRootFolder ) ) !== false )
		{
			if ( $file == ".." || $file == "." || $file == ".svn" )
			{
				echo " - skip: <span style=\"color: #aaa\">$folder$file</span><br/>\r\n";
				continue;
			}
			
			
			if ( is_file( $root.$folder.$file ) ) {
				$localfilename = $root.$folder.$file;
				$localfilename = str_replace($root.DIRECTORY_SEPARATOR, "", $localfilename);
				
				echo " + file: <span style=\"color: #000\"><b>$folder$file</b> - $localfilename</span><br/>\r\n";
				$ini->set( $folder, $file, md5($root.$folder.$file).".".sha1_file($root.$folder.$file) );
				
				$zip->addFile($root.$folder.$file, $localfilename);
				$filesCount++;
			}
			else
			{
				echo " + fold: <span style=\"color: #00a\"><b>$folder$file</b></span><br/>\r\n";
				$ini->addSection($folder.$file.DIRECTORY_SEPARATOR);
				addFolderToIni($folder.$file.DIRECTORY_SEPARATOR, $ini, $root, $zip);
			}
		}
	}
	else
	{
		echo("Can't read files from current directory!<br/>");
	}
	//echo "<span style=\"color:gray\">--- End</span><br/>";
}
*/

class Ini{ private $file; private $object; private $comments; public $defsection; public $array_var_to_object=false; public function __construct($file){$this->array_var_to_object=false;$this->comments=array();$this->file=$file;$this->defsection="Base";if(@file_exists($file)){$content=@file_get_contents($file);if($content!=false){$this->file=$file;$array=$this->toArray($content);$this->object=$array;}else {$this->object=array();}}else {if(@is_a($file,@get_class($this))){$this->object=$file->object;}else {$this->object=array();}}return $this;} private function toArray($code){$lines=explode("\r\n",$code);$array=array();$cursect="";foreach($lines as $line){if(preg_match("/^\s*?\[\s*(\w+)\s*\]\s*$/",$line)){$cursect=str_replace("[","",$line);$cursect=str_replace("]","",$cursect);$cursect=preg_replace("/^(\s*)/","",$cursect);$array[$cursect]=array();}elseif(preg_match("/^\s*(.*)\s*=\s*(.*)\s*?/",$line)){$cur=preg_replace("/(\s*=\s*)/","=",$line);$cur=preg_replace("/^(\s*)/","",$cur);list($var,$value)=explode("=",$cur);$array[$cursect][$var]=$this->thinkType($value);}else {continue;}}return $array;} private function toObject(array$array){$newobj=array();foreach($array as $item=>$val){if(gettype($val)=="array"){if($this->array_var_to_object){$newobj[$item]=(object)$this->toObject($val);;}else {$newobj[$item]=(object)$val;}}else {$newobj[$item]=$val;}}return (object)$newobj;} private function thinkType($value,$skipArray=false){if(preg_match("/\"(.*)\"/",$value)){$value=preg_replace('/(\s*\"(.*)\"\s*)/',"$2",$value);return (string)$value;}else {$value=preg_replace("/(\s*)/","",$value);if($value=="false"){return false;}elseif($value=="true"){return true;}elseif(preg_match("/^[0-9.-]+$/",$value)){return (double)$value;}else {return (string)$value;}}} private function thinkSaveArray($array){$code="";foreach($array as $val){switch(gettype($val)){case "array":$code.="".$this->thinkSaveArray($val).",";break;case "int":$code.="".$this->thinkSaveDouble($val).",";break;case "double":$code.="".$this->thinkSaveDouble($val).",";break;default:$code.="".$this->thinkSaveString($val).",";}}$code=preg_replace("/(,)$/","",$code);return $code;} private function thinkSaveString($string){return "\"".$string."\"";} private function thinkSaveDouble($double){return (double)$double;} public function dispatch($asObject=false){if($asObject){return $this->toObject($this->object);}return $this->object;} public function get($section,$key=null){if($key==null){$key=$section;$section=$this->defsection;}if(isset($this->object[$section][$key])){return $this->object[$section][$key];}} public function set($section,$key,$value=null){if($value==null){$key=$section;$section=$this->defsection;$value=$key;}$this->object[$section][$key]=$value;return $this;} public function add($section,$key,$value=null){if($value==null){$key=$section;$section=$this->defsection;$value=$key;}if(!isset($this->object[$section][$key])){$this->set($section,$key,$value);}return $this;} public function update($section,$key,$value=null){if($value==null){$key=$section;$section=$this->defsection;$value=$key;}if(isset($this->object[$section][$key])){$this->set($section,$key,$value);}return $this;} public function remove($section,$key=null){if($key==null){$key=$section;$section=$this->defsection;}if(isset($this->object[$section][$key])){unset($this->object[$section][$key]);}return $this;} public function addSection($section,$value=array()){if(!isset($this->object[$section])){$this->object[$section]=$value;}return $this;} public function removeSection($section){if(isset($this->object[$section])){unset($this->object[$section]);}return $this;} public function getSection($section){if(isset($this->object[$section])){return (array)($this->object[$section]);}return array();} public function setSection($section,$value=array()){$this->object[$section]=$value;return $this;} public function updateSection($section,$value){if(isset($this->object[$section])){foreach($value as $key=>$val){$this->object[$section][$key]=$val;}}return $this;} public function unionSections(){$args=func_get_args();$newsect=array();foreach($args as $section){$curarr=array();if(gettype($section)=="array"){$curarr=$section;}else {if(isset($this->object[$section])){$curarr=$this->object[$section];}else {continue;}}foreach($curarr as $item=>$val){$newsect[$item]=$val;}}return $newsect;} public function sortSection($section,$up=true){if(isset($this->object[$section])){if($up){ksort($this->object[$section]);}else {krsort($this->object[$section]);}}return $this;} public function sortAllSections($up=true){foreach($this->object as $section=>$value){$this->sortSection($section,$up);}return $this;} public function sortIni($up=true){if($up){ksort($this->object);}else {krsort($this->object);}return $this;} public function unionIni($iniobject,$level=0){if(@is_a($iniobject,@get_class($this))){foreach($iniobject->object as $section=>$value){if($level==1){$this->setSection($section,$this->unionSections($section,$iniobject->object[$section]));}elseif($level==2){$this->setSection($section,$value);}else {$this->addSection($section,$value);}}}return $this;} public function returnUnionSections(){$new=array();foreach($this->object as $section){$new=$this->unionSections($section,$new);}return $new;} public function clearIni(){unset($this->object);$this->object=array();return $this;} public function saveToFile($filename,$overwrite=true){$code="";foreach($this->object as $section=>$value){$code.="\r\n[".$section."]\r\n";foreach($value as $key=>$val){switch(gettype($val)){case "array":$code.=$key." = ".$this->thinkSaveArray($val)."\r\n";break;case "int":$code.=$key." = ".$this->thinkSaveDouble($val)."\r\n";break;case "double":$code.=$key." = ".$this->thinkSaveDouble($val)."\r\n";break;default:$code.=$key." = ".$this->thinkSaveString($val)."\r\n";}}}if(file_exists($filename)&&$overwrite){file_put_contents($filename,$code);}else {file_put_contents($filename,$code);}} public function save(){$this->saveToFile($this->file,true);} public function addComments($comments,$place="top"){return;}}


if ( !isset($_GET['install']) )
{
	header("Content-type: text/html; charset=utf8");
?>
<html>
<head>
<title>Phenol Engine Installer</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div style="padding: 9px;font-family: 'Courier New';font-size: 12px;">
List of install packs:<br /><br />
<?
if ( $handle = opendir( $searchFolder ) )
{
	while ( ( $file = readdir( $handle ) ) !== false )
	{
		if ( $file == ".." || $file == "." || $file == ".svn" ) continue;
		
		list($name, $ext) = explode(".", $file, 2);
		
		list($name, $data, $time) = explode("_", $name, 3);
		
		$data = explode("-", $data);
		$data = array_reverse($data);
		$data = implode(".", $data);
		
		$data = str_replace("-", ".", $data);
		$time = str_replace("-", ":", $time);
		
		if ( $ext == $installerExt )
		{
			echo "<a href=\"installer.php?install=$file\">Install</a> -> $name <span style=\"color: #aaa\">( $data $time )</span><br/><br/>\r\n";
		}
	}
}
else
{
	echo "Error of reading dir: $rootFolder<br/>";
}



?>
</div>
</body>
</html>
<?
}
else // if ( !isset($_GET['install']) )
{

die("installing");


define("VERSION", "0.1.1.12", true);

//include "engine/class/ini.class.php";

$dateFormat = date("Y-m-d_H-i-s");

$rootFolder = realpath(dirname("".__FILE__));
$saveFile = realpath(dirname(dirname("".__FILE__)))."\\installer_$dateFormat.ini";
$saveFileZip = realpath(dirname(dirname("".__FILE__)))."\\installer_$dateFormat.zip";
$saveInstaller = realpath(dirname(dirname("".__FILE__)))."\\installer_$dateFormat.phi";
$baseSection = "Phenol_Installer";
$filesCount = 0;

$manifest = new Ini("");
$manifest->addSection($baseSection);
$manifest->add($baseSection, "Name", "Phenol");
$manifest->add($baseSection, "Version", VERSION);
$manifest->add($baseSection, "Author", "LestaDz");
$manifest->add($baseSection, "Email", "lestad@bk.ru");
$manifest->add($baseSection, "Folder", $rootFolder);

$zip = new ZipArchive;
$zip->open($saveFileZip, ZipArchive::CREATE);

?>
<html>
<head>
<title>Log</title>
</head>
<body>
<div style="padding: 9px;font-family: 'Courier New';font-size: 12px;">
<?
echo " * root: $rootFolder<br/><br/>";
addFolderToIni(DIRECTORY_SEPARATOR, $manifest, $rootFolder, $zip);
echo "<br/> * fcnt: $filesCount files";
echo "<br/> * save: $saveFile";

$manifest->saveToFile($saveFile);

echo "<br/> * zipa: $saveFileZip";

$zip->close();

$installer = new ZipArchive;
$installer->open($saveInstaller, ZipArchive::CREATE);
$installer->addFile($saveFile, "manifest.ini");
$installer->addFile($saveFileZip, "content.phenol");
$installer->close();

echo "<br/> * packing...";
echo "<br/> * phin: $saveInstaller";
echo "<br/> * size: ". round(filesize($saveInstaller) / 1024, 2) . "KB";

?>
<br /><br />
<a href="file:///<?=str_replace("\\", "/", $saveInstaller)?>" target="_blank">Save Installer</a> | <a href="grabber.php">Back</a>
</div>
</body>
</html>
<?


} // if ( !isset($_GET['install']) )